package cn.well.cloud.modular.service.impl.thread;

import cn.well.cloud.base.wechat.model.NoticeInfo;
import cn.well.cloud.modular.model.NoticeLog;
import cn.well.cloud.modular.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Component
public class ThreadPool {


    /**
     * 创建发送通知线程池
     * @param noticeInfo
     * @param list
     * @param token
     * @param noticeLog
     * @throws InterruptedException
     */
    public static void createSendNoticePool(NoticeInfo noticeInfo, List<UserInfo> list, String token, NoticeLog noticeLog) throws InterruptedException{
        int count = 2;                   //一个线程处理300条数据
        int listSize = list.size();        //数据集合大小
        int runSize = (listSize/count)+1;  //开启的线程数
        List<UserInfo> newlist = null;       //存放每个线程的执行数据
        ExecutorService executor = Executors.newFixedThreadPool(runSize);      //创建一个线程池，数量和开启线程的数量一样
        //创建两个个计数器
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(runSize);
        //循环创建线程
        for (int i = 0; i < runSize ; i++) {
            //计算每个线程执行的数据
            if((i+1)==runSize){
                int startIndex = (i*count);
                int endIndex = list.size();
                newlist= list.subList(startIndex, endIndex);
            }else{
                int startIndex = (i*count);
                int endIndex = (i+1)*count;
                newlist= list.subList(startIndex, endIndex);
            }
            //线程类
            SendNoticeThread sendNoticeThread = new SendNoticeThread(noticeInfo, newlist, token, noticeLog,begin,end);
            //这里执行线程的方式是调用线程池里的executor.execute(mythead)方法。

            executor.execute(sendNoticeThread);
        }

        begin.countDown();
        end.await();

        //执行完关闭线程池
        executor.shutdown();
    }


    /**
     * 创建同步用户线程池
     * @param list
     * @param token
     * @param wxType
     * @throws InterruptedException
     */
    public static void createSyncUserInfoPool(List<String> list, String token,String wxType) throws InterruptedException{
        int count = 30;                   //一个线程处理300条数据
        int listSize = list.size();        //数据集合大小
        int runSize = (listSize/count)+1;  //开启的线程数
        List<String> newlist = null;       //存放每个线程的执行数据
        ExecutorService executor = Executors.newFixedThreadPool(runSize);      //创建一个线程池，数量和开启线程的数量一样
        //创建两个个计数器
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(runSize);
        //循环创建线程
        for (int i = 0; i < runSize ; i++) {
            //计算每个线程执行的数据
            if((i+1)==runSize){
                int startIndex = (i*count);
                int endIndex = list.size();
                newlist= list.subList(startIndex, endIndex);
            }else{
                int startIndex = (i*count);
                int endIndex = (i+1)*count;
                newlist= list.subList(startIndex, endIndex);
            }
            //线程类
            SyncUserInfoThread syncUserInfoThread = new SyncUserInfoThread(newlist, token,wxType, begin,end);
            //这里执行线程的方式是调用线程池里的executor.execute(mythead)方法。

            executor.execute(syncUserInfoThread);
        }

        begin.countDown();
        end.await();

        //执行完关闭线程池
        executor.shutdown();
    }

}
